package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.mongo.PublishApi;
import com.tanhua.server.Interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MovementsMQService {

    @Autowired(required = false)
    private RocketMQTemplate rocketMQTemplate;

    @Reference
    private PublishApi publishApi;



    /**
     * 发布动态消息
     */
    public void publishMsg(String publishId) {
        this.sendMsg(publishId, 1);
    }

    /**
     * 浏览动态消息
     */
    public void queryPublishMsg(String publishId) {
        this.sendMsg(publishId, 2);
    }

    /**
     * 点赞动态消息
     */
    public void likePublishMsg(String publishId) {
        this.sendMsg(publishId, 3);
    }

    /**
     * 取消点赞动态消息
     */
    public void disLikePublishMsg(String publishId) {
        this.sendMsg(publishId, 6);
    }

    /**
     * 喜欢动态消息
     */
    public void lovePublishMsg(String publishId) {
        this.sendMsg(publishId, 4);
    }

    /**
     * 取消喜欢动态消息
     */
    public void disLovePublishMsg(String publishId) {
        this.sendMsg(publishId, 7);
    }

    /**
     * 评论动态消息
     */
    public void commentPublishMsg(String publishId) {
        this.sendMsg(publishId, 5);
    }


    /**
     * 作用： 发送消息
     * @param publishId
     * @param type
     */
    public void sendMsg(String publishId,Integer type){
        //1. 找到朋友圈
        Publish publish = publishApi.findById(publishId);
        //2. 创建map对象存储消息
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("userId", UserHolder.getUserId());
        paramMap.put("publishId", publishId);
        paramMap.put("pid", publish.getPid());
        paramMap.put("type",type);

        //转换为json
        String jsonStr = JSON.toJSONString(paramMap);
        rocketMQTemplate.convertAndSend("tanhua-quanzi",jsonStr);

    }

}
